
import java.util.Scanner;
public class Test {
    public static void test22(int a) throws CloneNotSupportedException,ArithmeticException,NullPointerException {
        if(a == 10) {
            throw new CloneNotSupportedException();//受查异常  必须此时处理
        }
        System.out.println("11111");
    }
    public static void main14(String[] args) {
        try {
            test22(10);
        }catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        System.out.println("2");
//        int x = 10;
//        if(x == 10) {
//            throw new Exception();//默认是受查异常
//        }
    }
    //第二种处理方法对于throw
    public static void test2(int a) {
        try {
            if(a == 10) {
                throw new NullPointerException();
            }
        }catch (NullPointerException e) {
            e.printStackTrace();
        }
        System.out.println("1");
    }
    public static void main13(String[] args) {
        try {
            test2(10);
        }catch (NullPointerException e) {
            e.printStackTrace();
        }
        System.out.println("haha");
    }

    //第一种处理方法对于throw
    public static void test1(int a) {
      if(a == 10)
      throw new NullPointerException();
      System.out.println("1");
    }

    public static void main12(String[] args) {
        try {
            test1(10);
        }catch (NullPointerException e) {
            e.printStackTrace();
        }
        System.out.println("haha");
    }

    //和main1的写法是等价的。
    public static void main3(String[] args) {
        try {
            //System.out.println(10/0);
            int[] array = {1,2,3,4};
            //array = null;
            System.out.println(array[10]);
        }catch (ArithmeticException | NullPointerException | ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();//快速的定位异常出现的位置
            //System.out.println("你这里出现异常了！");
        }

        System.out.println("执行后续代码！");
    }
    //不处理异常
    public static void main2(String[] args) {
        System.out.println(10/0);
    }
    //处理异常
    public static void main1(String[] args) {
        try {
            System.out.println(10/0);
            int[] array = {1,2,3,4};
            array = null;
            System.out.println(array[10]);
        }catch (ArithmeticException e) {
            e.printStackTrace();//快速的定位异常出现的位置
            System.out.println("你这里出现算术异常了！");
        }catch (NullPointerException e) {
            e.printStackTrace();
            System.out.println("捕捉到了空指针异常！");
        }catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
            System.out.println("数组越界异常！");
        }
        System.out.println("执行后续代码！");
    }
}
